package com.eapi.eapisdk.Eapiclient;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.eapi.eapisdk.Eapiclient.model.InterfaceInfoInvokeRequest;
import com.eapi.eapisdk.Eapiclient.model.UserRequest;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

public class Eapiclient {
    private String accessKey;
    private String secretKey;
    private InterfaceInfoInvokeRequest interfaceInfoInvokeRequest;
    public Eapiclient(String accessKey, String secretKey,InterfaceInfoInvokeRequest interfaceInfoInvokeRequest){
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.interfaceInfoInvokeRequest = interfaceInfoInvokeRequest;
    }
    public static Map<String,String> generateKeyHeader(String accessKey, String secretKey, Long interfaceInfoId){
        Map<String,String> keyHeader = new HashMap<>();
        keyHeader.put("accessKey",accessKey);
        keyHeader.put("nonce", RandomUtil.randomNumbers(5));//随机数，length指的是生成随机数的长度
        String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
        keyHeader.put("timestamp",timeStamp);//时间戳
        keyHeader.put("sign", generateSign(timeStamp,secretKey));//签名算法
        keyHeader.put("interfaceInfoId", String.valueOf(interfaceInfoId));
        keyHeader.put("JWT", "zihan1");
        return keyHeader;
    }
    public static String generateSign(String timeStamp, String secretKey){
        Digester md5 = new Digester(DigestAlgorithm.SHA256);
        String content = timeStamp+ "." + secretKey;
        // 5393554e94bf0eb6436f240a4fd71282
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 对字符串进行字节转换并进行哈希计算
            byte[] messageDigest = md.digest(content.getBytes());
            // 将哈希后的字节数组转化为十六进制字符串
            StringBuilder digestHex = new StringBuilder();
            for (byte b : messageDigest) {
                digestHex.append(String.format("%02x", b));
            }
            return digestHex.toString();
        }catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found!", e);
        }
    }

    public String simpleCall(String url, UserRequest userequest){
        String UserJson = JSONUtil.toJsonStr(userequest);
        HttpRequest.post("http://121.40.232.157:7529/api/user/login")
                .contentType("application/json") // 设置请求体的内容类型
                .body(UserJson)
                .addHeaders(generateKeyHeader(accessKey,secretKey,interfaceInfoInvokeRequest.id))
                .execute();
        String json = JSONUtil.toJsonStr(interfaceInfoInvokeRequest);
        HttpResponse response = HttpRequest.post(url)
                .contentType("application/json") // 设置请求体的内容类型
                .body(json)
                .addHeaders(generateKeyHeader(accessKey,secretKey,interfaceInfoInvokeRequest.id))
                .execute();
        return response.body();

    }

}
